//
//  AboutDeals.m
//  欢乐美食
//
//  Created by 遇水 on 15/6/29.
//  Copyright (c) 2015年 遇水. All rights reserved.
//

#import "AboutDeals.h"
#import "AboutDealsCell.h"

@interface AboutDeals ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;   //表格数据

@property (nonatomic, strong) NSArray    *dataSource;

@end

@implementation AboutDeals


-(NSArray *)dataSource{
    if (_dataSource==nil) {

        _dataSource = [NSArray array];
    }
    
    return _dataSource;
}


-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self loadMoreDeals];
    }
    return self;
}

//更多
-(void)loadMoreDeals
{
    //发送一个网络请求
    MSFindDealsParam *param = [[MSFindDealsParam alloc]init];
    param.city = [[NSUserDefaults standardUserDefaults]stringForKey:@"city"];
    param.category = @"美食";
    param.limit = @(4);
    [MSDealTool findDeals:param success:^(MSFindDealsResult *result) {
        self.dataSource = result.deals;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
}



#pragma mark - tableView 代理方法

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"AboutDealsCell";
    AboutDealsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"AboutDealsCell" owner:nil options:nil]lastObject];
    }

    
    cell.deal = self.dataSource[indexPath.item];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MSDeals *deal = self.dataSource[indexPath.row];
    
    //发送一个通知   然后刷新数据
    if ([self.delegate respondsToSelector:@selector(updateHomeViewControllerView:dealId:)]) {
        [self.delegate updateHomeViewControllerView:self dealId:deal.deal_id];
    }
}


@end
